// localStorage数据存储服务
const STORAGE_KEYS = {
  USERS: 'users',
  JOBS: 'jobs',
  APPLICATIONS: 'applications'
};

// 生成唯一ID
function generateId() {
  return Date.now().toString(36) + Math.random().toString(36).substr(2);
}

// 从localStorage获取数据
function getData(key) {
  try {
    const data = localStorage.getItem(key);
    return data ? JSON.parse(data) : [];
  } catch (error) {
    console.error(`Error reading ${key} from localStorage:`, error);
    return [];
  }
}

// 向localStorage保存数据
function setData(key, data) {
  try {
    localStorage.setItem(key, JSON.stringify(data));
    return true;
  } catch (error) {
    console.error(`Error saving ${key} to localStorage:`, error);
    return false;
  }
}

// 用户相关操作
export function saveUser(user) {
  const users = getData(STORAGE_KEYS.USERS);
  users.push(user);
  return setData(STORAGE_KEYS.USERS, users);
}

export function findUserByUsername(username) {
  const users = getData(STORAGE_KEYS.USERS);
  return users.find(user => user.username === username) || null;
}

export function findAllUsers() {
  return getData(STORAGE_KEYS.USERS);
}

// 职位相关操作
export function saveJob(job) {
  const jobs = getData(STORAGE_KEYS.JOBS);
  jobs.push(job);
  return setData(STORAGE_KEYS.JOBS, jobs);
}

export function findJobById(id) {
  const jobs = getData(STORAGE_KEYS.JOBS);
  return jobs.find(job => job.id === id) || null;
}

export function findJobsByRecruiterId(recruiterId) {
  const jobs = getData(STORAGE_KEYS.JOBS);
  return jobs.filter(job => job.recruiterId === recruiterId);
}

export function findAllJobs() {
  return getData(STORAGE_KEYS.JOBS);
}

export function updateJob(id, updatedJob) {
  const jobs = getData(STORAGE_KEYS.JOBS);
  const index = jobs.findIndex(job => job.id === id);
  if (index !== -1) {
    jobs[index] = { ...jobs[index], ...updatedJob };
    return setData(STORAGE_KEYS.JOBS, jobs);
  }
  return false;
}

export function deleteJob(id) {
  const jobs = getData(STORAGE_KEYS.JOBS);
  const filteredJobs = jobs.filter(job => job.id !== id);
  return setData(STORAGE_KEYS.JOBS, filteredJobs);
}

// 申请相关操作
export function saveApplication(application) {
  const applications = getData(STORAGE_KEYS.APPLICATIONS);
  applications.push(application);
  return setData(STORAGE_KEYS.APPLICATIONS, applications);
}

export function findApplicationById(id) {
  const applications = getData(STORAGE_KEYS.APPLICATIONS);
  return applications.find(application => application.id === id) || null;
}

export function findApplicationsByJobId(jobId) {
  const applications = getData(STORAGE_KEYS.APPLICATIONS);
  return applications.filter(application => application.jobId === jobId);
}

export function findApplicationsByApplicantId(applicantId) {
  const applications = getData(STORAGE_KEYS.APPLICATIONS);
  return applications.filter(application => application.applicantId === applicantId);
}

export function findAllApplications() {
  return getData(STORAGE_KEYS.APPLICATIONS);
}

export function updateApplication(id, updatedApplication) {
  const applications = getData(STORAGE_KEYS.APPLICATIONS);
  const index = applications.findIndex(application => application.id === id);
  if (index !== -1) {
    applications[index] = { ...applications[index], ...updatedApplication };
    return setData(STORAGE_KEYS.APPLICATIONS, applications);
  }
  return false;
}

// 初始化数据（如果需要）
export function initializeStorage() {
  // 确保存储键存在
  if (!localStorage.getItem(STORAGE_KEYS.USERS)) {
    setData(STORAGE_KEYS.USERS, []);
  }
  if (!localStorage.getItem(STORAGE_KEYS.JOBS)) {
    setData(STORAGE_KEYS.JOBS, []);
  }
  if (!localStorage.getItem(STORAGE_KEYS.APPLICATIONS)) {
    setData(STORAGE_KEYS.APPLICATIONS, []);
  }
}

// 导出工具函数
export { generateId };